#include "AnimationMdiArea.h"
#include "delegate/AnimationDelegate.h"
#include "delegate/ProcessDelegate.h"

#include <QDialog>
#include <QHBoxLayout>
#include <QLabel>

#include <QProcess>
#include <QWindow>
#include <windows.h>
#include <iostream>
#include <QApplication>
#include <QProcess>
#include <QTimer>
#include <QFileDialog>
#include <QDateTime>
#include <QMainWindow>
#include <string.h>
#include "util/FileUtil.h"
AnimationMdiArea::AnimationMdiArea()
{

}



AnimationMdiArea::AnimationMdiArea(
    QWidget* parent):
    QMdiArea(parent)
{
    this->setViewMode(ViewMode::TabbedView);
    this->add_sub_widget ();
}



AnimationMdiArea::~AnimationMdiArea()
{}



void
AnimationMdiArea::add_sub_widget()
{
    d_earth_3D = new QMdiSubWindow;
    d_earth_3D->setWindowTitle(tr ("aaaaa"));
    this->setTabsClosable(true);
    this->setTabShape(QTabWidget::TabShape::Triangular);
    d_earth_2D = new QMdiSubWindow;
  //  AnimationDelegate* delegt = new AnimationDelegate (this,this);

   // QLabel* lab = new QLabel(tr ("bbbbbbb"));

   // QDialog* tool_2d = new QDialog (this);
  // QHBoxLayout* lay = new QHBoxLayout(this);
  //  lay->addWidget(lab);
    //lay->addWidget(tool_2d);
    //tool_2d->setLayout(lay);
  //  tool_2d->show();

    //this->setLayout(lay);
    //this->addSubWindow(d_earth_3D);
    //this->addSubWindow(d_earth_2D);

    //this->cascadeSubWindows();





#if 0


    QString cmd="F:/solarSystem.exe";
       STARTUPINFO si={sizeof(si)};
       PROCESS_INFORMATION pi;
       si.dwFlags=STARTF_USESHOWWINDOW;
       si.wShowWindow=true;
/*
      bool bRet=CreateProcess(
                   NULL,
                   (LPWSTR)cmd.toStdWString().c_str(),
                   NULL,
                   NULL,
                   FALSE,
                   CREATE_NEW_CONSOLE,
                   NULL,
                   NULL,&si,&pi);

       Sleep(2000);
       */
       QProcess* Process = new QProcess(this);
      // Process->start ("F:/solarSystem.exe");
//Sleep(2000);
       WId wid = (WId)FindWindow(L"UnityWndClass",NULL);
       std::cout << "wid = " << wid << std::endl;
       //WId wid = (WId)FindWindow(L"Rgui Workspace",NULL);
       QWindow *m_window;
       m_window = QWindow::fromWinId(wid);
       m_window->setFlags(m_window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint); //


       QWidget* widget;

       widget = createWindowContainer(m_window,this);
       widget->setWindowState(Qt::WindowMaximized);
       attachWindowThreadInput(wid);
       SetForegroundWindow((HWND)wid);
       this->addSubWindow(widget);
        HWND hFocus = GetFocus();
       PostMessage(hFocus, WM_KEYDOWN,
                   VK_LBUTTON , 1);

    //   QWidget* second_widget;
      // second_widget = createWindowContainer(m_window,second_widget);
     //  second_widget->setLayout(lay);
      // this->addSubWindow(second_widget);
       //QWidget *m_widget;
       //m_widget = QWidget::createWindowContainer(m_window,this->ui->widget);

      // widget->setMaximumSize(400,300);

#endif

#if 0



       HWND hWnd = (HWND)FindWindow(NULL,L"SolarSystem");

       if(hWnd)
               {
                   HWND hForeWnd = ::GetForegroundWindow();
                   DWORD dwForeID = ::GetWindowThreadProcessId(hForeWnd,NULL);
                   DWORD dwCurID = ::GetCurrentThreadId();
                   ::AttachThreadInput(dwCurID,dwForeID,TRUE);
                   ::ShowWindow(hWnd,SW_SHOWNORMAL);
                   ::SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
                   ::SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,0,0, SWP_NOSIZE|SWP_NOMOVE);
                   ::SetForegroundWindow(hWnd);
                   ::AttachThreadInput(dwCurID,dwForeID,FALSE);
                  // return 0;
               }

       QWindow *m_window;
       m_window = QWindow::fromWinId((WId)hWnd);
       m_window->setFlags(m_window->flags() | Qt::CustomizeWindowHint | Qt::WindowTitleHint); //


       QWidget* widget;

       widget = createWindowContainer(m_window,this);
       widget->setWindowState(Qt::WindowMaximized);
       SwitchToThisWindow (hWnd,true);
     //  attachWindowThreadInput(wid);
     //  SetForegroundWindow((HWND)wid);
       this->addSubWindow(widget);
#endif

#if 0

       QProcess* process = new QProcess;
       process->startDetached("F:/solarSystem.exe");
           WId wid = 0;
           do{
               QEventLoop loop;
               QTimer::singleShot(1, &loop, SLOT(quit()));
               loop.exec();
               wid = (WId)FindWindow(L"UnityWndClass", NULL);
             }while(wid==0);
             QWindow *m_window;
             m_window = QWindow::fromWinId(wid);
             QWidget *widget = QWidget::createWindowContainer(m_window,this);
             widget->setWindowState(Qt::WindowMaximized);
             this->addSubWindow(widget)->setWindowTitle("Missile Model");
#endif


         ProcessDelegate* p_timer = new ProcessDelegate (this,this);


}






void
AnimationMdiArea::attachWindowThreadInput(WId remoteWindowId)
{
    DWORD currentThreadId = GetCurrentThreadId();
    DWORD remoteThreadId = GetWindowThreadProcessId((HWND)remoteWindowId, NULL);

    //qDebug() << __FUNCTION__ << "win" << remoteWindowId << "thr" << remoteThreadId;
    if (currentThreadId != remoteThreadId) {
        bool status = AttachThreadInput(currentThreadId, remoteThreadId, true);
        std::cout << "status = " << status << std::endl;
    }
}
